package xhr03;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
/**
* @Description: 生产者发送消息
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/8/16 15:09
*/
public class Producer {

    public static void main(String[] args) throws Exception {
        // 1.创建生产者对象
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producer_hello_group");
        // 2.设置NameServer地址,均衡调用对象
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动producer实例
        defaultMQProducer.start();
        // 4.创建消息
        Message message = new Message("hello_topic2", "hello_tags2", "我在源码时代学习java异步发送~~~".getBytes(StandardCharsets.UTF_8));
        // 5.发送异步消息消息
        defaultMQProducer.send(message, new SendCallback() {
            // 消息发送成功
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }
            // 消息发送失败
            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("消息发送失败!");
            }
        });
    }

}
